package org.onion.pattern.event.spring.consumer.log;


import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.onion.pattern.event.consumer.log.AsyncEventConsumeLogStorage;
import org.onion.pattern.event.consumer.log.EventConsumeLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Component
@Slf4j
public class JdbcEventConsumeLogStorage extends AsyncEventConsumeLogStorage {

    @Setter
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String SQL_INSERT = "INSERT INTO _event_consumed_log (" +
            "event_key, consume_status, channel, topic, sub_topic, reconsume_times," +
            "event_time, published_time, receive_time, consumed_time, born_time_stamp," +
            "msg_id, consumer_group, queue_id, broker, message_offset," +
            "properties, payload, exception_stack" +
            ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    public JdbcEventConsumeLogStorage() {
        //TODO 使用配置 2025-08-18 21:27  何挺强
        super(1000, 100, 500);
    }

    @Override
    protected void batchSave(List<EventConsumeLog> consumeLogs) {
        // 使用 jdbcTemplate.batchUpdate 执行批量操作
        jdbcTemplate.batchUpdate(SQL_INSERT, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                EventConsumeLog consumeLog = consumeLogs.get(i);
                // 注意：这里的 setXXX 顺序必须和 SQL 语句中的列顺序完全一致！

                ps.setString(1, consumeLog.getEventKey());
                ps.setString(2, consumeLog.getConsumeStatus() != null ? consumeLog.getConsumeStatus().toString() : null);
                ps.setString(3, consumeLog.getChannel());
                ps.setString(4, consumeLog.getTopic());
                ps.setString(5, consumeLog.getSubTopic());
                ps.setInt(6, consumeLog.getReconsumeTimes()); // 使用 setObject 处理可能为 null 的 Integer

                ps.setObject(7, consumeLog.getEventTime());
                ps.setObject(8, consumeLog.getPublishedTime());
                ps.setObject(9, consumeLog.getReceiveTime());
                ps.setObject(10, consumeLog.getConsumedTime());
                ps.setObject(11, null); //consumeLog.getBornTimeStamp()

                ps.setString(12, consumeLog.getMsgId());
                ps.setString(13, consumeLog.getConsumerGroup());
                ps.setObject(14, null); //consumeLog.getQueueId()
                ps.setString(15, null); //consumeLog.getBroker()
                ps.setObject(16, null); //consumeLog.getMessageOffset()

                ps.setString(17, null); //consumeLog.getProperties()
                ps.setString(18, null); //consumeLog.getPayload()
                ps.setString(19, consumeLog.getExceptionStack());
            }

            @Override
            public int getBatchSize() {
                return consumeLogs.size();
            }
        });

    }


}
